1  Python/Jupyter Crash Course

Open In Colab

Some of you might have only little experience with Python and Jupyter notebooks. This introductory tutorial will help you to get started.

Jupyter notebooks

Jupyter notebooks are web-based interactive computing platforms that allow you to create and share documents that contain live code, equations, visualizations and narrative text. They are commonly used for data analysis, scientific computing, and machine learning. Here are some steps to get started with Jupyter notebooks:

  • Running code: In a Jupyter notebook, you can write and run code in cells. To run a cell, simply click on it and press Shift + Enter or click the “Run” button in the toolbar.

  • Markdown cells: In addition to code cells, you can also create Markdown cells to write descriptive text. You can format the text using Markdown syntax to add headings, lists, links, etc..

  • Saving and exporting: To save your notebook, click on the “Save” button in the toolbar. Jupyter notebooks have the extension .ipynb and can be exported to other formats like HTML, PDF, etc.

  • Keyboard Shortcuts: Jupyter notebooks have several keyboard shortcuts that can make your work faster and more efficient. For example, you can press Esc to enter command mode and Enter to enter edit mode. The most frequently used Keyboard Shortcuts are: b for creating a cell below the current cell, a for creating a cell above the current cell, dd for deleting the current cell.

Google Colab

Colab allows you to use Jupyter notebooks with others without having to download, install, or run anything.

Introduction to Python

If you have not too much experience with Python programming, we strongly recommend to go through some online tutorials, such as the ones from Kaggle Learn. This will help you to get started. Here is just a brief overview of the basic concepts.

Variables

Variables are used to store values in a program. You can assign values to variables using the = operator. For example (you can run it using SHIFT + ENTER or clicking on Run):

x = 5 # assign 5 to the variable x
y = 10 # assign 10 to the variable y
z = x + y # add x and y, and assign the value to z
print(z) # Output: 15

Data Types

Python has several built-in data types, including numbers (integer, float), strings, lists, tuples, and dictionaries. For example:

# Numbers
x = 5
y = 5.5

# Strings
name = "John Doe"

# Lists
fruits = ["apple", "banana", "cherry"]

# Tuples
person = ("John", "Doe", 30)

# Dictionaries
person = {
    "first_name": "John",
    "last_name": "Doe",
    "age": 30
}

Operators

Python supports several types of operators, including arithmetic, comparison, and assignment operators. For example:

# Arithmetic
x = 5
y = 3
z = x + y
print(z) # Output: 8

# Comparison
x = 5
y = 3
print(x > y) # Output: True

# Assignment
x = 5
x += 3
print(x) # Output: 8

Conditional Statements

Conditional statements allow you to control the flow of a program based on certain conditions. You can play around by changing the value of x.

x = 5
if x > 10:
    print("x is greater than 10")
else:
    print("x is less than or equal to 10")

Loops

Loops allow you to repeat a block of code multiple times. There are two types of loops in Python: for and while loops.

# For Loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# Output:
# apple
# banana
# cherry
# While Loop
x = 0
while x < 5:
    print(x)
    x += 1

# Output:
# 0
# 1
# 2
# 3
# 4

Functions

Functions are reusable blocks of code that perform a specific task. You can define functions using the def keyword.

def greet(name):
    print("Hello, " + name)

greet("Philippe") # Output: Hello, Philippe

Additional examples

You can copy any of that code to make it run in a code cell.

Numbers

# Addition
x = 5
y = 10
z = x + y
print(z) # Output: 15

# Subtraction
x = 15
y = 10
z = x - y
print(z) # Output: 5

# Multiplication
x = 5
y = 10
z = x * y
print(z) # Output: 50

# Division
x = 15
y = 10
z = x / y
print(z) # Output: 1.5

# Modulo
x = 15
y = 10
z = x % y
print(z) # Output: 5

# Exponentiation
x = 5
y = 2
z = x ** y
print(z) # Output: 25

Just create a code cell below to run selected examples.

Strings

first_name = "John"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name) # Output: John Smith

# Repetition
x = "hello"
y = x * 3
print(y) # Output: hellohellohello

# Indexing
x = "hello"
print(x[0]) # First element -> Output: h
print(x[-1]) # Last element -> Output: o 

# Slicing
x = "hello"
print(x[1:4]) # Output: ell
print(x[:3]) # Output: hel
print(x[1:]) # Output: ello

# Membership
x = "hello"
print("h" in x) # Output: True
print("z" in x) # Output: False

Lists

# Indexing
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry

# Slicing
fruits = ["apple", "banana", "cherry"]
print(fruits[1:3]) # Output: ['banana', 'cherry']

# Membership
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # Output: True
print("orange" in fruits) # Output: False

# Length
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # Output: 3

# Append
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']

# Insert
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "orange")
print(fruits) # Output: ['apple', 'orange', 'banana', 'cherry']

# Remove
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry']

# Pop
fruits = ["apple", "banana", "cherry"]
x = fruits.pop()
print(x) # Output: cherry
print(fruits) # Output: ['apple', 'banana']

Tuples

# Indexing
person = ("John", "Doe", 30)
print(person[0]) # Output: John
print(person[-1]) # Output: 30

# Slicing
person = ("John", "Doe", 30)
print(person[1:3]) # Output: ('Doe', 30)

# Membership
person = ("John", "Doe", 30)
print("John" in person) # Output: True
print("Jane" in person) # Output: False

# Length
person = ("John", "Doe", 30)
print(len(person)) # Output: 3

Dictionaries

# Indexing
person = {
    "first_name": "John",
    "last
last_name": "Doe",
    "age": 30
}
print(person["first_name"]) # Output: John

# Membership
person = {
    "first_name": "John",
    "last_name": "Doe",
    "age": 30
}
print("first_name" in person) # Output: True
print("email" in person) # Output: False

# Length
person = {
    "first_name": "John",
    "last_name": "Doe",
    "age": 30
}
print(len(person)) # Output: 3

# Adding key-value pairs
person = {
    "first_name": "John",
    "last_name": "Doe",
    "age": 30
}
person["email"] = "johndoe@email.com"
print(person) # Output: {'first_name': 'John', 'last_name': 'Doe', 'age': 30, 'email': 'johndoe@email.com'}

# Modifying values
person = {
    "first_name": "John",
    "last_name": "Doe",
    "age": 30
}
person["first_name"] = "Jane"
print(person) # Output: {'first_name': 'Jane', 'last_name': 'Doe', 'age': 30}

# Removing key-value pairs
person = {
    "first_name": "John",
    "last_name": "Doe",
    "age": 30
}
del person["age"]
print(person) # Output: {'first_name': 'John', 'last_name': 'Doe'}

This is just a brief introduction to Python programming. There are many more advanced topics and features in Python, but these basics should give you a good start.

If you want more exercises for basic Python Programming go through the Intro to Programming and Python courses on Kaggle Learn.